package com.example.service.impl;

import com.example.domain.po.Keyword;
import com.example.mapper.KeywordMapper;
import com.example.service.KeywordService;
import com.example.util.PageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class KeywordServiceImpl implements KeywordService {

    @Autowired
    private KeywordMapper keywordMapper;
    @Override
    public PageBean<Keyword> list(Integer pageNum, Integer pageSize, String name) {
        PageBean<Keyword> pb = new PageBean<>();
        PageHelper.startPage(pageNum, pageSize);
        List<Keyword> list = keywordMapper.list(name);
        Page<Keyword> p = (Page<Keyword>)list ;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return pb;

    }

    @Override
    public void add(Keyword keyword) {
        System.out.println(keyword);
        if (keyword.getId()==null){

            keywordMapper.add(keyword);
        }else {
            keywordMapper.update(keyword);
        }
    }

    @Override
    public List<Map<String, Object>> getNameAndId() {
        return keywordMapper.getNameAndId();
    }

    @Override
    public Keyword getById(Integer id) {
        return keywordMapper.getById(id);
    }
}
